package com.bcx.crm.java.httpServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by ys on 2017/12/28.
 */
//使用@WebServlet将一个继承于javax.servlet.http.HttpServlet的类定义为servlet组件
@WebServlet(urlPatterns = "/mainServlet")
public class CountServlet extends HttpServlet {
    @Override
    public void init() throws ServletException {
        System.out.println("下面的参数是在当前servlet中存放的");//web.xml中自定义的Servlet中的init-param下<param-value>的值
        System.out.println(this.getInitParameter("servletParam"));//contextConfigLocation:Servlet中的init-param下<param-name>的值
        System.out.println("下面的参数是存放在servletContext中的");//web.xml的context-param
        System.out.println(getServletContext().getInitParameter("contextConfigLocation"));
        super.init();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取ServletContext的引用
        ServletContext servletContext = getServletContext();
        Integer count =(Integer) servletContext.getAttribute("count");
        if (null == count){
            System.out.println("context中还没有count属性呢");
            count=0;
        }
        count++;
        servletContext.setAttribute("count",count);
        System.out.println("您是第"+count+"个访问servlet的");
//        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }

    @Override
    public void destroy() {
        super.destroy();
    }
}
